clear all 
cd "~\Data\Open_Close_Auctions"
global output `"~/Results"'

use auction_price_impact2021.dta, clear 
keep if year(date)>2011
 
merge 1:1 permno date using merged_permno_date_from_step2_sample_noAmex.dta

keep if _merge==3

drop _merge

sort permno date
xtset permno date


gen pct_oi=(close_imb_qty)/adv_10d
gen signed_pct_oi1=pct_oi  if inlist(close_imb_side, "B")
replace signed_pct_oi1=-1*pct_oi if close_imb_side=="S"
replace signed_pct_oi1=0 if missing(signed_pct_oi1)

* change the treatment of outliers where the imbalance is beyond 10% of the ADV
replace signed_pct_oi1=. if signed_pct_oi1>0.1
replace signed_pct_oi1=. if signed_pct_oi1<-0.1
gen closing_auction_oib=signed_pct_oi1


gen signed_pct_open_oi=imbalance/adv_10d if inlist(imbalance_side, "B")
replace signed_pct_open_oi=-1*imbalance/adv_10d if inlist(imbalance_side, "S")
replace signed_pct_open_oi=0 if missing(signed_pct_open_oi)

replace signed_pct_open_oi=. if signed_pct_open_oi>0.1
replace signed_pct_open_oi=. if signed_pct_open_oi<-0.1
gen open_auction_oib=signed_pct_open_oi

gen openinterest_count=imbalance+paired
gen ocount=1 if !missing(openinterest_count) & openinterest_count!=0


 
gen subsample_group=1 if   !missing(lag1_me) & lag1_me > me_cutoff50
replace subsample_group=2 if missing(subsample_group) & !missing(lag1_me) & lag1_me < me_cutoff50 & lag1_me >= me_cutoff20
replace subsample_group=3 if missing(subsample_group) & !missing(lag1_me) & lag1_me < me_cutoff20

gen DummySmall=1 if subsample_group==2
gen DummyLarge=1 if subsample_group==1
gen DummyMicro=1 if subsample_group==3
replace DummySmall=0 if missing(DummySmall)
replace DummyLarge=0 if missing(DummyLarge)
replace DummyMicro=0 if missing(DummyMicro)


* closing auction return

gen ret_closing_auction=(abs(prc)-price1545)/price1545
replace ret_closing_auction=. if ret_closing_auction>0.10 & !missing(ret_closing_auction)
replace ret_closing_auction=. if ret_closing_auction<-0.10 & !missing(ret_closing_auction)
replace ret_closing_auction=ret_closing_auction*1
replace ret_closing_auction=. if missing(prc) | missing(price1545)

* open auction return 
gen ret_open0928=(2*oprc-(before928ask+before928bid))/((before928ask+before928bid))
replace ret_open0928=. if ret_open0928>0.10 & !missing(ret_open0928)
replace ret_open0928=. if ret_open0928<-0.10 & !missing(ret_open0928)
replace ret_open0928=ret_open0928*1
replace ret_open0928=. if missing(before928ask) | missing(before928bid) | missing(oprc)


** both open and close auction: meaning that the stock has open and close auction and we set the dependent variable to be the missing if no open auction so that it will not enter into the estimate 
replace ret_closing_auction=. if ocount !=1

gen sp500=1 if !missing(sp500_flag)
replace sp500=0 if missing(sp500)


* have lagged volatility
gsort permno date
by permno: gen vol22d_lag1=vol_22d[_n-1]
gen vol_22d_ann=vol22d_lag1*sqrt(252)
replace vol_22d_ann=2 if vol_22d_ann>2
replace vol_22d_ann=0.01 if vol_22d_ann<0.01
* change to lag(Volat) instead of lag(LogVolat)
*gen logvol22d_ann=log(vol_22d_ann)


replace price_lag1=abs(price_lag1)
replace price_lag1=0.5 if price_lag1<0.5
replace price_lag1=300 if price_lag1>300
gen price_measure=-1*log(price_lag1)


gen nasd=1 if exchcd==3
replace nasd=0 if exchcd==1


gen dow = dow(date)
gen EA_dummy1=0 if missing(ea_dummy)
replace EA_dummy1=1 if missing(EA_dummy1)

gen Friday=1 if dow==5
replace Friday=0 if missing(Friday)

gen signed_sqrt_oi=sign(closing_auction_oib)*((abs(closing_auction_oib))^0.5)


replace ret_closing_auction=100*ret_closing_auction
replace ret_open0928=100*ret_open0928

gen yearmonth = 100*year(date)+month(date)
gsort permno yearmonth + date 
 
preserve 
* first stage time-series regression
*count of observations  within permno yearmonth for signed_sqrt_oi value equals to 0 
bys permno yearmonth: gen count_signed_sqrt_oi_0 = sum(signed_sqrt_oi == 0)
bys permno yearmonth: replace count_signed_sqrt_oi_0 = count_signed_sqrt_oi_0[_N]

bys permno yearmonth: asreg ret_closing_auction signed_sqrt_oi
bys permno yearmonth: egen sum_signed_sqrt_oi=total(signed_sqrt_oi)
bys permno yearmonth: egen sum_ret_closing_auction=total(ret_closing_auction)

* filters in place 
bys permno yearmonth: keep if missing(_R2)==0
bys permno yearmonth: keep if _Nobs>10
bys permno yearmonth: replace _b_signed_sqrt_oi=. if count_signed_sqrt_oi_0>15
bys permno yearmonth: drop if sum_signed_sqrt_oi==0
bys permno yearmonth: drop if sum_ret_closing_auction==0
bys permno yearmonth: keep if _n==1

* generate a variable that is equal to the average of the last 12 months of _b_signed_sqrt_oi 

bys permno: gen counttime=_n 
rangestat (mean) lambda_lag12m = _b_signed_sqrt_oi, interval(counttime -12 -1) by(permno)
* set lambda_lag12m to missing if the counttime is less than 12
replace lambda_lag12m=. if counttime<12


gen volatile=vol_22d_ann
gen inverseprice=price_measure

gen dummylarge_nasd=DummyLarge*nasd
gen dummysmall_nasd=DummySmall*nasd
gen dummymicro_nasd=DummyMicro*nasd

gen dummylarge_volatile=DummyLarge*vol_22d_ann
gen dummysmall_volatile=DummySmall*vol_22d_ann
gen dummymicro_volatile=DummyMicro*vol_22d_ann

gen dummylarge_inverseprice=DummyLarge*price_measure
gen dummysmall_inverseprice=DummySmall*price_measure
gen dummymicro_inverseprice=DummyMicro*price_measure

winsor2 _b_signed_sqrt_oi lambda_lag12m, cuts(1  99)

xtset permno yearmonth

drop _Nobs _R2 _adjR2 _b_cons


******************Table 3 regressions************************
eststo clear 
eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd, noconstant fmb newey(12) 
eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd dummymicro_volatile dummysmall_volatile dummylarge_volatile  , noconstant fmb newey(12) 
eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd dummymicro_volatile dummysmall_volatile dummylarge_volatile dummymicro_inverseprice dummysmall_inverseprice dummylarge_inverseprice, noconstant  fmb newey(12) 
 

esttab  using `"${output}\Table3.csv"', scalars("N N" "r2_a Adj.R-sq") sfmt(%-8.0f %-8.3f) title("Time-sereies Two stage Regression with different subsample dummy for square root models for closing auction : Square root model: first stage is univariate time-series regression within a month and run cross sectional regression with characterisitcs from the beginning of year ") append indicate(`r(indicate_fe)') starlevels(* 0.10 ** 0.05 *** 0.01) nostar nogaps


******************Table 5 regressions************************


*for opening auctions
*for opening auctions
*for opening auctions
*for opening auctions

preserve 
gen signed_sqrt_oi_open=sign(open_auction_oib)*((abs(open_auction_oib))^0.5)

* first stage time-series regression
*count of observations  within permno yearmonth for signed_sqrt_oi value equals to 0 
bys permno yearmonth: gen count_signed_sqrt_oi_0 = sum(signed_sqrt_oi_open == 0)
bys permno yearmonth: replace count_signed_sqrt_oi_0 = count_signed_sqrt_oi_0[_N]

bys permno yearmonth: asreg ret_open0928 signed_sqrt_oi_open
bys permno yearmonth: egen sum_signed_sqrt_oi=total(signed_sqrt_oi_open)
bys permno yearmonth: egen sum_ret_open0928=total(ret_open0928)

* filters in place 
bys permno yearmonth: keep if missing(_R2)==0
bys permno yearmonth: keep if _Nobs>10
bys permno yearmonth: replace _b_signed_sqrt_oi_open=. if count_signed_sqrt_oi_0>15
bys permno yearmonth: drop if sum_signed_sqrt_oi==0
bys permno yearmonth: drop if sum_ret_open0928==0
bys permno yearmonth: keep if _n==1

* generate a variable that is equal to the average of the last 12 months of _b_signed_sqrt_oi 
* generate a variable that is equal to the average of the last 12 months of _b_signed_sqrt_oi

bys permno: gen counttime=_n 
rangestat (mean) lambda_lag12m = _b_signed_sqrt_oi_open, interval(counttime -12 -1) by(permno)
* set lambda_lag12m to missing if the counttime is less than 12
replace lambda_lag12m=. if counttime<12


gen volatile=vol_22d_ann
gen inverseprice=price_measure

gen dummylarge_nasd=DummyLarge*nasd
gen dummysmall_nasd=DummySmall*nasd
gen dummymicro_nasd=DummyMicro*nasd

gen dummylarge_volatile=DummyLarge*vol_22d_ann
gen dummysmall_volatile=DummySmall*vol_22d_ann
gen dummymicro_volatile=DummyMicro*vol_22d_ann

gen dummylarge_inverseprice=DummyLarge*price_measure
gen dummysmall_inverseprice=DummySmall*price_measure
gen dummymicro_inverseprice=DummyMicro*price_measure

winsor2 _b_signed_sqrt_oi_open lambda_lag12m, cuts(1  99)

xtset permno yearmonth

drop _Nobs _R2 _adjR2 _b_cons

eststo clear 
eststo: asreg _b_signed_sqrt_oi_open_w DummyMicro DummySmall DummyLarge , noconstant fmb newey(12) 
eststo: asreg _b_signed_sqrt_oi_open_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd, noconstant fmb newey(12) 
eststo: asreg _b_signed_sqrt_oi_open_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd dummymicro_volatile dummysmall_volatile dummylarge_volatile  , noconstant fmb newey(12) 
eststo: asreg _b_signed_sqrt_oi_open_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd dummymicro_volatile dummysmall_volatile dummylarge_volatile dummymicro_inverseprice dummysmall_inverseprice dummylarge_inverseprice, noconstant  fmb newey(12) 
 
esttab  using `"${output}\Table5.csv"', scalars("N N" "r2_a Adj.R-sq") sfmt(%-8.0f %-8.3f) title("Time-sereies Two stage Regression with different subsample dummy for square root models for opening auction : Square root model: first stage is univariate time-series regression within a month and run cross sectional regression with characterisitcs from the beginning of year ") append indicate(`r(indicate_fe)') starlevels(* 0.10 ** 0.05 *** 0.01) nostar nogaps